use crate::{Address, Byte, Result};

/// BASIC ROM 模块
pub struct BasicRom {
    data: Vec<Byte>,
    start_address: Address,
}

impl BasicRom {
    pub fn new() -> Result<Self> {
        // 这里将加载原始的 BASIC 解释器二进制代码
        // 目前使用空数据，后续将从文件加载
        let data = vec![0; 8192]; // 8KB
        
        Ok(BasicRom {
            data,
            start_address: 0xA000, // BASIC ROM 的标准起始地址
        })
    }

    pub fn read(&self, address: Address) -> Result<Byte> {
        if address >= self.start_address && address < self.start_address + self.data.len() as Address {
            let index = (address - self.start_address) as usize;
            Ok(self.data[index])
        } else {
            Err(crate::EmulatorError::InvalidMemoryAccess(address))
        }
    }

    pub fn start_address(&self) -> Address {
        self.start_address
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_rom() {
        let rom = BasicRom::new().unwrap();
        
        // 测试起始地址
        assert_eq!(rom.start_address(), 0xA000);
        
        // 测试读取（目前全是0）
        assert_eq!(rom.read(0xA000).unwrap(), 0);
        assert_eq!(rom.read(0xBFFF).unwrap(), 0);
        
        // 测试边界条件
        assert!(rom.read(0x9FFF).is_err());
        assert!(rom.read(0xC000).is_err());
    }
}